﻿using System;
using sx_windows.pane.wifi;
using sx_windows.data.repository;
using sx_windows.utils;
using static sx_windows.utils.CMD;
using NETCONLib;
using sx_windows.resource;

namespace sx_windows
{
    public class WifiSettingPresenter : WifiSettingContract.Presenter
    {
        private WifiSettingRepository wifiSettingRepository;
        private WifiSettingContract.View mView;
        private static bool isWifiOpened;
        public WifiSettingPresenter(WifiSettingContract.View view) {
            this.mView = view;
            wifiSettingRepository = WifiSettingRepository.Instance();
        }
        public void init()
        {
            string name = wifiSettingRepository.GetWifiConfig().Name;
            string password = wifiSettingRepository.GetWifiConfig().Password;
            mView.SetWifiName(name);
            mView.SetWifiPassword(password);
        }

        public void OpenWifiBtnClick()
        {
            if (isWifiOpened)
            {
                isWifiOpened = false;
                CloseWifi();
                return;
            }
            string name = mView.GetWifiName();
            string password = mView.GetWifiPassword();
            wifiSettingRepository.SaveWifiConfig(name, password);
            if (TextUtil.IsEmpty(name))
            {
                mView.SetOpenWifiBtnText("开启");
                mView.ShowBottomText("wifi名不得为空");
                return;
            }
            if (!TextUtil.WifiPasswordValid(password))
            {
                mView.SetOpenWifiBtnText("开启");
                mView.ShowBottomText("wifi密码输入有误");
                return;
            }
            CreateWifiAdapter(name,password);
        }

        private void CloseWifi()
        {
            CMD.execute("netsh wlan stop hostednetwork");
        }

        private void CreateWifiAdapter(string name, string password)
        {
            mView.ShowBottomText("正在创建热点");
            string cmd = String.Format("netsh wlan set hostednetwork mode=allow ssid={0} key={1}", name, password);
            string result = CMD.execute(cmd);
            if (((result.IndexOf("承载网络模式已设置为允许") > -1) && (result.IndexOf("已成功更改承载网络的 SSID。") > -1)) && (result.IndexOf("已成功更改托管网络的用户密钥密码。") > -1))
            {
                OpenWifi();
            }
            else
            {
                mView.ShowBottomText("创建失败，请检查无线网卡");
                mView.SetOpenWifiBtnText("开启");
            }
        }

        private void OpenWifi()
        {
            mView.ShowBottomText("正在启动wifi热点");
            string cmd = "netsh wlan start hostednetwork";
            string result = CMD.execute(cmd);
            if (result.IndexOf("已启动承载网络") > -1)
            {
                mView.ShowBottomText("正在配置网络连接");
                cmd = "systeminfo";
                result = CMD.execute(cmd);
                int index = result.IndexOf("Microsoft Hosted Network Virtual Adapter");
                if (index <= -1)
                {
                    mView.ShowBottomText("启动失败");
                    mView.SetOpenWifiBtnText("开启");
                    return;
                }
                result = result.Substring(index);
                string startStr = "连接名:      ";
                string endStr = "\r\n                      启用 DHCP";
                int startIndex = result.IndexOf(startStr) + startStr.Length;
                int endIndex = result.IndexOf(endStr);
                if (result.IndexOf(startStr) <= -1 || endIndex < startIndex)
                {
                    mView.ShowBottomText("启动失败");
                    mView.SetOpenWifiBtnText("开启");
                    return;
                }
                string adapterName = result.Substring(result.IndexOf(startStr) + startStr.Length, endIndex - startIndex);
                StartWifiConfig(adapterName);
            }
            else
            {
                mView.ShowBottomText("启动失败");
                mView.SetOpenWifiBtnText("开启");
            }
        }

        private void StartWifiConfig(string adapterName)
        {
            try
            {
                NetSharingManager manager = new NetSharingManager();
                var connections = manager.EnumEveryConnection;
                foreach (INetConnection c in connections)
                {
                    var props = manager.NetConnectionProps[c];
                    var sharingCfg = manager.INetSharingConfigurationForINetConnection[c];
                    if (props.Name ==Const.APP_NAME)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
                    }
                    else if (props.Name == adapterName)
                    {
                        sharingCfg.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
                    }
                }
                mView.ShowBottomText("已开启wifi共享");
                mView.SetOpenWifiBtnText("关闭");
                isWifiOpened = true;
            }
            catch
            {
                mView.SetOpenWifiBtnText("开启");
                mView.ShowBottomText("请使用管理员权限运行");
            }
        }

        public void WifiUsageLinkClick()
        {
            System.Diagnostics.Process.Start("http://wifi.mandroid.cn");
        }
    }
}